package com.formula.datastructure.exercise.tree;

/**
 * insert放在node里面
 * traverse放在node里面
 */
public class BinSearchNode {
    public Integer data;
    public BinSearchNode left;
    public BinSearchNode right;

    public BinSearchNode(Integer data) {
        this.data = data;
    }

    public void insert(BinSearchNode newNode) {
        if (newNode.data < data) {
            if (left == null) {
                left = newNode;
            } else {
                left.insert(newNode);
            }
        } else if (newNode.data > data) {
            if (right == null) {
                right = newNode;
            } else {
                right.insert(newNode);
            }
        } else {
            System.out.println("Data has existed.");
        }
    }

    public void inOrderTraverse() {
        if (left != null) {
            left.inOrderTraverse();
        }
        visit();
        if (right != null) {
            right.inOrderTraverse();
        }
    }

    private void visit() {
        System.out.println(data);
    }
}
